package cn.itcast.order.service;

import cn.itcast.feign.client.UserClient;
import cn.itcast.feign.pojo.User;
import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;
//    @Autowired
//    private RestTemplate restTemplate;
    @Autowired
    private UserClient userClient;

    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        String url="http://userservice/user/"+order.getUserId();
        //2.远程调用查询用户接口
        //User user = restTemplate.getForObject(url, User.class);
        //利用feign发送http请求，查询用户
        User user = userClient.queryById(order.getUserId());
        /**原理：
         * 根据FeignClient的value属性获取服务名，到本地缓存中查询服务名userservice
         * 根据Ribbon负载均衡在服务列表中选择合适的服务得到服务地址信息，默认是ZoneAvoidanceRule（区域轮询策略）
         * 根据@GetMapping("/{id}")和类上的@RequestMapping("/user")跟到具体的请求地址/user/1
         * 利用userClient发送http请求，得到响应结果，解析成user对象
         */
        //3.封装order中的user属性
        order.setUser(user);
        // 4.返回
        return order;
    }
}
